/**
 * 
 */
package com.jie.eclipse.pan.editors.assist.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jst.jsp.ui.internal.contentassist.JSPContentAssistProcessor;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;

import com.jie.eclipse.pan.Activator;
import com.jie.eclipse.pan.editors.assist.contributor.AbstractAssistantContributor;
import com.jie.eclipse.pan.editors.assist.contributor.BaseFormItemAssContributor;
import com.jie.eclipse.pan.editors.assist.contributor.IAssistantContributor;
import com.jie.eclipse.pan.editors.utils.StructuredModelUtil;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 *
 */
public class BaseJSPContentAssistProcessor extends JSPContentAssistProcessor{
	private Map<String ,IAssistantContributor> acMap = null;
	
	private boolean isOffsetValid(ITextViewer viewer, int offset) {
		return true;
//		try {
//			IStructuredDocument structuredDocument = (IStructuredDocument)viewer.getDocument();
//			
//			//判断分区类型
//			if (IJSPPartitions.JSP_DIRECTIVE != structuredDocument.getPartition(offset).getType()) 
//				return false;
//			
//			//判断叶子text region对应的region type信息
//			ITextRegion textRegion = StructuredDocumentUtil.getTextRegion(structuredDocument, offset);
//			return DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE == textRegion.getType();
//		} catch (Exception e) {
//			return false;
//		}
	}
	
	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
			int offset) {
		List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
		ICompletionProposal[] pps2 = getExtcomputeCompletionProposals(viewer,offset);
		for (int i = 0; pps2 != null && i < pps2.length; i++) {
			proposals.add(pps2[i]);
		}
		
		ICompletionProposal[] pps1 = super.computeCompletionProposals(viewer, offset);
		for (int i = 0; i < pps1.length; i++) {
			proposals.add(pps1[i]);
		}
		return proposals.toArray(new ICompletionProposal[proposals.size()]);
	}
	
	@SuppressWarnings("restriction")
	public ICompletionProposal[] getExtcomputeCompletionProposals(ITextViewer viewer,
			int offset){
		if (!this.isOffsetValid(viewer, offset))
			return new ICompletionProposal[0];
		
		//利用IModelManager获取对应的模型
		IStructuredDocument structuredDocument = (IStructuredDocument)viewer.getDocument();
		IStructuredModel structuredModel = StructuredModelManager.getModelManager().getModelForRead(structuredDocument);
		if (structuredModel == null) return null;
		try {
			//如果当前offset不是位于属性区域
			IDOMElement element  = StructuredModelUtil.getElementAtOffset(structuredModel, offset);
			IDOMAttr attrNode = StructuredModelUtil.getAttrAtOffset(structuredModel, offset);
			return this.computeCustomizedCompletionProposals(viewer, offset, element,attrNode);
		} catch (Exception e) {
			IStatus status = new Status(IStatus.ERROR, "jspeditor", 100, "自动提示失败", e);
			Activator.getDefault().getLog().log(status);
		} finally {
			//注意，削减引用计数
			if (structuredModel != null)
				structuredModel.releaseFromRead();
		}
		return null;
	}
	
	private ICompletionProposal[] computeCustomizedCompletionProposals(ITextViewer viewer, int offset,IDOMElement element, IDOMAttr attrNode) throws Exception{
		if(element == null) return null;
		//准备上下文数据
		String tagName = element.getNodeName();
		String attrbuteName = "";
		String inputText = "";
		if(attrNode != null){
			attrbuteName = attrNode.getName();
			int startOffset = attrNode.getValueRegionStartOffset();
			if(startOffset==0){
				startOffset = attrNode.getNameRegionStartOffset()-1;
			}
			inputText = attrNode.getStructuredDocument().get(startOffset + 1, offset - startOffset - 1);
		}
		//Utils.print(tagName+"----"+attrbuteName+"-----"+inputText);
		//获取相应通过扩展点注册的属性值提示扩展
		IAssistantContributor contributor = getIAssistantContributorByTagName(tagName);
		if (contributor == null)
			return new ICompletionProposal[0]; 
		
		return contributor.computeProposals(element, attrNode,tagName,attrbuteName, inputText, viewer, offset);
	}
	
	public IAssistantContributor getIAssistantContributorByTagName(String tagName){
		if(acMap == null)acMap = new HashMap<String ,IAssistantContributor>();
		
		IAssistantContributor ac = null;
		if(acMap.containsKey(tagName)){
			ac = acMap.get(tagName);
		}else{
			ac = new BaseFormItemAssContributor(); 
			acMap.put(tagName, ac);
		}
		return ac;
	}
	
}
